// pages/mine/mine.js
const app = getApp()
import api from '../../config/api';
import {
    GetStudentgrades, // 查询我的成绩
    GetByOpenId, // 查询用户信息
} from '../../api/index'

import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl:api.BaseImgUrl,
     levelfeatureArray:[{title:'我的志愿',image:'mine_icon_volunteer.png',type:'wish'},
                        {title:'我的收藏',image:'mine_icon_collect.png',type:'collect'},
                        {title:'邀请好友',image:'mine_icon_invite.png',type:'invite'},
                        {title:'卡密激活',image:'mine_icon_card.png',type:'activate'}],
     featureListArray:[{title:'帮助中心',type:'help'},
                        {title:'关于我们',type:'about'},
                        {title:'用户协议',type:'agreement'},
                        {title:'问题反馈',type:'question'},
                        {title:'退出登录',type:'logout'}],
    imageBase64:'http://47.92.28.18:5099/img/applet/mine_icon_vipback.png',
    score:'',
    subject:"",
    position:'',
    token :'',
    show:false,
    headImage:'',
    nickname:'',
    pici:'本科批',
    openVip:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  // 登录
  loginAction() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  /** 编辑成绩 */
  editGradeAction() {
      wx.navigateTo({
        url: '/pages/mine/editgrade/editgrade',
      })
  },

  /** 功能点击事件 */
  levelSelectAction(event) {
      console.log(event)
      let token = wx.getStorageSync('token') || '';
      if (token == '') {
          wx.navigateTo({
            url: '/pages/login/login',
          })
          return
      }
      let itemType = event.currentTarget.dataset.type
    let url = ''
    if (itemType == 'wish') { // 我的志愿
        //   url = '/pages/mine/mywish/mywish'
        // url = '/pages/index/currentdesire/currentdesire'
        url= '/pages/index/currentdesire/currentdesire?type=' + this.data.pici
    } else if (itemType == 'collect') { // 我的收藏
      url = '/pages/mine/mycollect/mycollect'
    } else if (itemType == 'activate') { // 卡密激活
        if (this.data.openVip == 1) {
            Toast('您已开通VIP')
            return
        }

        url = '/pages/mine/cardactivate/cardactivate'
    } else if (itemType == 'invite') { // 邀请好友
        this.setData({
            show:true
        })
        return
    }
    wx.navigateTo({
      url: url,
    })
      
  },

  onClose() {
    this.setData({
        show:false
    })
  },

  // 功能列表点击事件
  featureListAction(event) {
      console.log('点击')
      let itemType = event.currentTarget.dataset.type
      let url = ''
      if (itemType == 'question') { // 问题反馈
            url = '/pages/mine/feedback/feedback'
      } else if (itemType == 'help') { //  帮助中心
            url = '/pages/mine/rich/rich?type=0'
      }  else if (itemType == 'agreement') { //  用户协议
        url = '/pages/mine/rich/rich?type=2'
      }  else if (itemType == 'about') { //  关于我们
        url = '/pages/mine/rich/rich?type=1'
      } else if (itemType == 'logout') { // 退出登录
        let token = wx.getStorageSync('token') || '';
        if (token == '') {
            Toast('请先登录')
            return
        }
        let that = this;
        Dialog.confirm({
            title: '退出登录',
            message: '是否退出登录',
        })
        .then(() => {
            // on confirm
            wx.removeStorageSync('token');
            wx.removeStorageSync('openId');
            wx.removeStorageSync('headimg');
            wx.removeStorageSync('nickname');
            that.setData({
                subject:'',
                token :'',
            })
        })
        .catch(() => {
            // on cancel
        });
        return
      }
      wx.navigateTo({
        url: url,
      })
  },

  // 开通vip
  openVipAction() {
    wx.navigateTo({
        url: '/pages/mine/openvip/openvip',
      })
  },

  // 刷新成绩
  refreshAction() {
    let openid = wx.getStorageSync('openId') || '';
    if (openid != '') {
        this._GetStudentgrades('?openId=' + openid, false)
    }
  },

  // 客服点击事件
  kefuAction() {
    wx.makePhoneCall({
        phoneNumber: '18704631100'
      })
  },

   // 我的成绩查询
   _GetStudentgrades(data, isLoading) {
    GetStudentgrades(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            let mutableArray = [];
            if (array.length != 0) {
                let model = array[0]
                if (model.isWuli == 1) {
                    mutableArray.push('物理')
                } else {
                    mutableArray.push('历史')
                }
                if (model.isHuaxue == 1) {
                    mutableArray.push('化学')
                }
                if (model.isShengwu == 1) {
                    mutableArray.push('生物')

                }
                if (model.isZhengzhi == 1) {
                    mutableArray.push('政治')
                }
                if (model.isDili == 1) {
                    mutableArray.push('地理')
                }
                let str = mutableArray.join('/'); // 使用逗号分隔
                this.setData({
                    score:model.myScore,
                    subject:str,
                    position:model.myWeici,
                    pici:model.reserve1
                })
            }
        } else {
            // Toast(res.msg)
        }
    })
},

_GetByOpenId(data, isLoading) {
    GetByOpenId(data, isLoading).then(res => {
        console.log(res)
        let that = this;
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                openVip:res.data.status
            })
        } else {
            Toast(res.msg)
        }
    })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      console.log('请求成绩')
    let openid = wx.getStorageSync('openId') || '';
    let token = wx.getStorageSync('token') || '';
    let headimg = wx.getStorageSync('headimg') || '';
    let nickname = wx.getStorageSync('nickname') || '';
    this.setData({
        token:token,
        headimg:headimg,
        nickname:nickname,
    })
    console.log(openid)
        if (openid != '') {
            this._GetStudentgrades('?openId=' + openid, false)
            this._GetByOpenId({openId:openid}, false)
        }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  onShareTimeline: function (){

},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})